/*
* pps_bom
*/
package com.pitop.mould.ctrl;


import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;

import com.pitop.core.exceptions.BaseException;
import com.pitop.core.entity.BeanRet;
import com.pitop.core.entity.Page;
import com.pitop.core.tools.redis.RedisUtils;
import com.pitop.mould.entity.MouldMaterial;
import com.pitop.mould.service.MouldMaterialSVImpl;

/**
 * 物料模具关联 控制器
 *
 * @author berton
 */
@RestController
@RequestMapping("/mouldMaterial")
@Slf4j
@Api(value = "物料模具关联控制器", description = "物料模具关联控制器")
public class MouldMaterialCtrl {

    @Resource
    protected RedisUtils redisUtils;
    @Autowired
    private MouldMaterialSVImpl mouldMaterialSVImpl;


    /**
     * 查询MouldMaterial一个详情信息
     *
     * @param id
     * @param materialCode 物料编码
     * @param mouldCode    模具编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询MouldMaterial一个详情信息", notes = "查询MouldMaterial一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "query"),
            @ApiImplicitParam(name = "materialCode", value = "物料编码", dataType = "java.lang.String", paramType = "query"),
            @ApiImplicitParam(name = "mouldCode", value = "模具编码", dataType = "java.lang.String", paramType = "query")
    })
    @GetMapping(value = "/load")
    @ResponseBody
    public BeanRet load(Long id, String materialCode, String mouldCode) {
        if (id == null) {
            return null;
        }
        if (materialCode == null) {
            return null;
        }
        if (mouldCode == null) {
            return null;
        }
        MouldMaterial mouldMaterial = mouldMaterialSVImpl.load(id, materialCode, mouldCode);
        log.info(JSON.toJSONString(mouldMaterial));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, mouldMaterial);
    }

    /**
     * 查询MouldMaterial一个详情信息
     *
     * @param id
     * @param materialCode 物料编码
     * @param mouldCode    模具编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询MouldMaterial一个详情信息", notes = "查询MouldMaterial一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "query"),
            @ApiImplicitParam(name = "materialCode", value = "物料编码", dataType = "java.lang.String", paramType = "query"),
            @ApiImplicitParam(name = "mouldCode", value = "模具编码", dataType = "java.lang.String", paramType = "query")
    })
    @GetMapping(value = "/get")
    @ResponseBody
    public BeanRet get(Long id, String materialCode, String mouldCode) {
        if (id == null) {
            return null;
        }
        if (materialCode == null) {
            return null;
        }
        if (mouldCode == null) {
            return null;
        }
        MouldMaterial mouldMaterial = mouldMaterialSVImpl.get(id, materialCode, mouldCode);
        log.info(JSON.toJSONString(mouldMaterial));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, mouldMaterial);
    }


    /**
     * 根据条件id查询MouldMaterial一个详情信息
     *
     * @param id
     * @return BeanRet
     */
    @ApiOperation(value = "查询MouldMaterial一个详情信息", notes = "查询MouldMaterial一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "path")
    })
    @GetMapping(value = "/load/id/{id}")
    @ResponseBody
    public BeanRet loadById(@PathVariable Long id) {
        if (id == null || id == 0) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        MouldMaterial mouldMaterial = mouldMaterialSVImpl.loadById(id);
        log.info(JSON.toJSONString(mouldMaterial));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, mouldMaterial);
    }

    /**
     * 根据条件materialCode查询MouldMaterial一个详情信息
     *
     * @param materialCode 物料编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询MouldMaterial一个详情信息", notes = "查询MouldMaterial一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "materialCode", value = "物料编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/load/materialCode/{materialCode}")
    @ResponseBody
    public BeanRet loadByMaterialCode(@PathVariable String materialCode) {
        if (StringUtils.isEmpty(materialCode)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        MouldMaterial mouldMaterial = mouldMaterialSVImpl.loadByMaterialCode(materialCode);
        log.info(JSON.toJSONString(mouldMaterial));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, mouldMaterial);
    }

    /**
     * 根据条件mouldCode查询MouldMaterial一个详情信息
     *
     * @param mouldCode 模具编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询MouldMaterial一个详情信息", notes = "查询MouldMaterial一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mouldCode", value = "模具编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/load/mouldCode/{mouldCode}")
    @ResponseBody
    public BeanRet loadByMouldCode(@PathVariable String mouldCode) {
        if (StringUtils.isEmpty(mouldCode)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        MouldMaterial mouldMaterial = mouldMaterialSVImpl.loadByMouldCode(mouldCode);
        log.info(JSON.toJSONString(mouldMaterial));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, mouldMaterial);
    }

    /**
     * 根据条件id查询MouldMaterial一个详情信息
     *
     * @param id
     * @return BeanRet
     */
    @ApiOperation(value = "查询MouldMaterial一个详情信息", notes = "查询MouldMaterial一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "path")
    })
    @GetMapping(value = "/get/id/{id}")
    @ResponseBody
    public BeanRet getById(@PathVariable Long id) {
        if (id == null || id == 0) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        MouldMaterial mouldMaterial = mouldMaterialSVImpl.getById(id);
        log.info(JSON.toJSONString(mouldMaterial));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, mouldMaterial);
    }

    /**
     * 根据条件materialCode查询MouldMaterial一个详情信息
     *
     * @param materialCode 物料编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询MouldMaterial一个详情信息", notes = "查询MouldMaterial一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "materialCode", value = "物料编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/get/materialCode/{materialCode}")
    @ResponseBody
    public BeanRet getByMaterialCode(@PathVariable String materialCode) {
        if (StringUtils.isEmpty(materialCode)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        MouldMaterial mouldMaterial = mouldMaterialSVImpl.getByMaterialCode(materialCode);
        log.info(JSON.toJSONString(mouldMaterial));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, mouldMaterial);
    }

    /**
     * 根据条件mouldCode查询MouldMaterial一个详情信息
     *
     * @param mouldCode 模具编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询MouldMaterial一个详情信息", notes = "查询MouldMaterial一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mouldCode", value = "模具编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/get/mouldCode/{mouldCode}")
    @ResponseBody
    public BeanRet getByMouldCode(@PathVariable String mouldCode) {
        if (StringUtils.isEmpty(mouldCode)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        MouldMaterial mouldMaterial = mouldMaterialSVImpl.getByMouldCode(mouldCode);
        log.info(JSON.toJSONString(mouldMaterial));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, mouldMaterial);
    }

    /**
     * 查询MouldMaterial信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询MouldMaterial信息集合", notes = "查询MouldMaterial信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),

    })
    @GetMapping(value = "/list")
    @ResponseBody
    public BeanRet list(@ApiIgnore MouldMaterial mouldMaterial, @ApiIgnore Page<MouldMaterial> page) {
        if (page == null) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        List<MouldMaterial> mouldMaterials = mouldMaterialSVImpl.list(mouldMaterial, page.getCurPage(), page.getPageSize());
        int total = mouldMaterialSVImpl.count(mouldMaterial);
        page.setTotalRow(total);
        page.setVoList(mouldMaterials);
        log.info(JSON.toJSONString(page));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, "", page);
    }

    /**
     * 查询MouldMaterial信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询MouldMaterial信息集合", notes = "查询MouldMaterial信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "materialCode", value = "物料编码", paramType = "query"),
            @ApiImplicitParam(name = "mouldCode", value = "模具编码", paramType = "query")
    })
    @GetMapping(value = "/list/by")
    @ResponseBody
    public BeanRet listByPk(Long id, String materialCode, String mouldCode, @ApiIgnore Page<MouldMaterial> page) {
        if (page == null) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        List<MouldMaterial> mouldMaterials = mouldMaterialSVImpl.list(id, materialCode, mouldCode, page.getCurPage(), page.getPageSize());
        int total = mouldMaterialSVImpl.count(id, materialCode, mouldCode);
        page.setTotalRow(total);
        page.setVoList(mouldMaterials);
        log.info(JSON.toJSONString(page));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, "", page);
    }

    /**
     * 创建MouldMaterial
     *
     * @return BeanRet
     */
    @ApiOperation(value = "创建MouldMaterial", notes = "创建MouldMaterial")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "materialCode", value = "物料编码", paramType = "query"),
            @ApiImplicitParam(name = "mouldCode", value = "模具编码", paramType = "query")
    })
    @PostMapping("/build")
    @ResponseBody
    public BeanRet build(@ApiIgnore MouldMaterial mouldMaterial) {
        if (StringUtils.isEmpty(mouldMaterial.getMaterialCode())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(mouldMaterial.getMouldCode())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        mouldMaterialSVImpl.save(mouldMaterial);
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, mouldMaterial);
    }

    /**
     * 修改MouldMaterial
     *
     * @return BeanRet
     */
    @ApiOperation(value = "修改MouldMaterial", notes = "修改MouldMaterial")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "materialCode", value = "物料编码", paramType = "query"),
            @ApiImplicitParam(name = "mouldCode", value = "模具编码", paramType = "query")
    })
    @PutMapping("/modify")
    @ResponseBody
    public BeanRet modify(@ApiIgnore MouldMaterial mouldMaterial) {
        if (StringUtils.isEmpty(mouldMaterial.getMaterialCode())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(mouldMaterial.getMouldCode())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        mouldMaterialSVImpl.modify(mouldMaterial);
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, mouldMaterial);
    }

    /**
     * 根据主键删除MouldMaterial
     *
     * @return BeanRet
     */
    @ApiOperation(value = "删除MouldMaterial", notes = "删除MouldMaterial")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "materialCode", value = "物料编码", paramType = "query"),
            @ApiImplicitParam(name = "mouldCode", value = "模具编码", paramType = "query")
    })
    @DeleteMapping("/delete")
    @ResponseBody
    public BeanRet delete(Long id, String materialCode, String mouldCode) {
        if (id != null && StringUtils.isEmpty(materialCode) && StringUtils.isEmpty(mouldCode)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }

        mouldMaterialSVImpl.delete(id, materialCode, mouldCode);
        return BeanRet.create(true, "删除MouldMaterial成功");
    }

}
